<?php
/*csimpe
 * Created on 2013-7-7
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class admin extends CI_Controller {

    function __construct() {
        parent::__construct();
     if(!login_user()){
       show_jump('已退出登录', site_url('manage/login/index'));
       }
        $this->load->model('manage/m_admin');
    }

	//管理员列表
    function index(){
    	if(!IS_POST){
	    	$data['result'] = $this->m_admin->get_list(0,50);
	    	$data['count'] = $this->m_admin->get_admin_count();
	    	$data['url']=base_url().'index.php/manage/admin/';
	    	$this->load->view('manage/admin_index',$data);
    	}else{
			$ids=$_POST['ids'];
			$ids=implode(',',$ids);
			$ids=trim($ids,',');
			$success=$this->m_admin->delete_list($ids);
	    	if($success){
    	 		show_jump('删除成功',site_url('manage/admin/index')) 	;
    	    }else{
       	    	show_jump('删除失败',site_url('manage/admin/index')) 	;
    	    }
    	}
    }

    function delete(){
    	$id = $_GET['id'];
    	$success= $this->m_admin->delete($id);
    	 if($success){
    	 	 show_jump('删除成功',site_url('manage/admin/index')) 	;
    	 }else{
       	   show_jump('删除失败',site_url('manage/admin/index')) 	;
    	 }
    }

    function add(){
    	if(!IS_POST){
    		$data['act'] = 'add';
    		$data['result'] = null;
    		$this->load->view('manage/admin_form',$data);
    	}else{
    		$admin_name = $_POST['admin_name'];
    	//	$admin_pwd = md5($_POST['admin_pwd']);
    		$this->load->library('PwdEncryption'); 
    		$p = new PwdEncryption();    		
    		$admin_pwd_array = $p->genEncryptPwd($_POST['admin_pwd']);
    		$admin_pwd=$admin_pwd_array[0];
    		$admin_salt=$admin_pwd_array[1];
    	//	print_r($admin_pwd);exit;
    		$admin_email = $_POST['admin_email'];
    		$admin_phone = $_POST['admin_phone'];
    		$enable = $_POST['enable'];
    		$success= $this->m_admin->add($admin_name,$admin_pwd,$admin_salt,$admin_email,$admin_phone,$enable);
    		 if($success){
	    	 	 show_jump('添加成功',site_url('manage/admin/index')) 	;
	    	 }else{
	       	   show_jump('添加失败',site_url('manage/admin/add')) 	;
	    	 }
    	}
    }

    function edit(){
    	$id = $_GET['id'];
    	//print_r($id);
    	if(!IS_POST){
    		$data['act'] = 'edit';
    		$data['result'] = $this->m_admin->get_admin($id);
    		$this->load->view('manage/admin_form',$data);
    	}else{
    		$admin_name = $_POST['admin_name'];
    		$this->load->library('PwdEncryption'); 
    		$p = new PwdEncryption();    		
    		$admin_pwd_array = $p->genEncryptPwd($_POST['admin_pwd']);
    		$admin_pwd=$admin_pwd_array[0];
    		$admin_salt=$admin_pwd_array[1];
    		$admin_email = $_POST['admin_email'];
    		$admin_phone = $_POST['admin_phone'];
    		$enable = $_POST['enable'];
    		$success= $this->m_admin->edit($id,$admin_name,$admin_pwd,$admin_salt,$admin_email,$admin_phone,$enable);
    		 if($success){
	    	 	 show_jump('修改成功',site_url('manage/admin/index')) 	;
	    	 }else{
	       	   	 show_jump('修改失败',site_url('manage/admin/edit?id='.$id)) 	;
	    	 }
    	}
    }
}
?>
